
<html lang=""><head>
    <title>条件渲染 · MINA</title>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
    <meta name="description" content="">
    <meta name="generator" content="GitBook 3.2.0">




    <link rel="stylesheet" href="../../../../gitbook/style.css">




    <link rel="stylesheet" href="../../../../gitbook/gitbook-plugin-highlight/website.css">



    <link rel="stylesheet" href="../../../../gitbook/gitbook-plugin-search/search.css">























    <meta name="HandheldFriendly" content="true">
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../../../../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../../../../gitbook/images/favicon.ico" type="image/x-icon">


    <link rel="next" href="list.html">


    <link rel="prev" href="data.html">


</head>
<body>

<div class="book with-summary">
    <div>

        <div class="sub_nav_box">
            <div class="sub_nav_inner">




                <ul class="top_sub_nav">




                    <li class="sub_nav_item " data-level="1.1" data-path="../../../">

                        <a href="../../../">


                            简易教程

                        </a>

                    </li>

                    <li class="sub_nav_item selected" data-level="1.2" data-path="../../MINA.html">

                        <a href="../../MINA.html">


                            框架

                        </a>

                    </li>

                    <li class="sub_nav_item " data-level="1.3" data-path="../../../component/">

                        <a href="../../../component/">


                            组件

                        </a>

                    </li>

                    <li class="sub_nav_item " data-level="1.4" data-path="../../../api/">

                        <a href="../../../api/">


                            API

                        </a>

                    </li>

                    <li class="sub_nav_item " data-level="1.5" data-path="../../../devtools/devtools.html">

                        <a href="../../../devtools/devtools.html">


                            工具

                        </a>

                    </li>

                    <li class="sub_nav_item " data-level="1.6" data-path="../../../qa/qa.html">

                        <a href="../../../qa/qa.html">


                            Q&amp;A

                        </a>

                    </li>




                </ul>




                <!-- <h1>条件渲染</h1> -->
                <div id="book-search-input" role="search">
                    <form>
                        <label for="search-input" class="search-icon" id="js-search-icon"></label>
                        <input type="text" id="search-input" name="search-input" placeholder="搜索">
                        <button type="reset" class="clear-input-btn"></button>
                    </form>
                </div>
            </div>
        </div>

        <div class="book-summary">


            <nav role="navigation">





                <ul class="summary">


                    <li class="chapter  " data-level="1.2.1" data-path="../../structure.html">

                        <a href="../../structure.html">


                            目录结构

                        </a>



                    </li>

                    <li class="chapter  " data-level="1.2.2" data-path="../../config.html">

                        <a href="../../config.html">


                            配置

                        </a>



                    </li>

                    <li class="chapter  open" data-level="1.2.3" data-path="../../app-service/">

                        <a href="../../app-service/">


                            逻辑层

                        </a>



                        <ul class="articles">


                            <li class="chapter  " data-level="1.2.3.1" data-path="../../app-service/app.html">

                                <a href="../../app-service/app.html">


                                    注册程序

                                </a>



                            </li>

                            <li class="chapter  " data-level="1.2.3.2" data-path="../../app-service/page.html">

                                <a href="../../app-service/page.html">


                                    注册页面

                                </a>



                            </li>

                            <li class="chapter  " data-level="1.2.3.3" data-path="../../app-service/module.html">

                                <a href="../../app-service/module.html">


                                    模块化

                                </a>



                            </li>

                            <li class="chapter  " data-level="1.2.3.4" data-path="../../app-service/api.html">

                                <a href="../../app-service/api.html">


                                    API

                                </a>



                            </li>


                        </ul>

                    </li>

                    <li class="chapter  open" data-level="1.2.4" data-path="../">

                        <a href="../">


                            视图层

                        </a>



                        <ul class="articles">


                            <li class="chapter  " data-level="1.2.4.1" data-path="./">

                                <a href="./">


                                    WXML

                                </a>



                                <ul class="articles">


                                    <li class="chapter  " data-level="1.2.4.1.1" data-path="data.html">

                                        <a href="data.html">


                                            数据绑定

                                        </a>



                                    </li>

                                    <li class="chapter active " data-level="1.2.4.1.2" data-path="conditional.html">

                                        <a href="conditional.html">


                                            条件渲染

                                        </a>



                                    </li>

                                    <li class="chapter  " data-level="1.2.4.1.3" data-path="list.html">

                                        <a href="list.html">


                                            列表渲染

                                        </a>



                                    </li>

                                    <li class="chapter  " data-level="1.2.4.1.4" data-path="template.html">

                                        <a href="template.html">


                                            模板

                                        </a>



                                    </li>

                                    <li class="chapter  " data-level="1.2.4.1.5" data-path="event.html">

                                        <a href="event.html">


                                            事件

                                        </a>



                                    </li>

                                    <li class="chapter  " data-level="1.2.4.1.6" data-path="import.html">

                                        <a href="import.html">


                                            引用

                                        </a>



                                    </li>


                                </ul>

                            </li>

                            <li class="chapter  " data-level="1.2.4.2" data-path="../wxss.html">

                                <a href="../wxss.html">


                                    WXSS

                                </a>



                            </li>

                            <li class="chapter  " data-level="1.2.4.3" data-path="../component.html">

                                <a href="../component.html">


                                    组件

                                </a>



                            </li>


                        </ul>

                    </li>


                </ul>


            </nav>


        </div>

        <div class="book-body">

            <div class="body-inner">

                <!--

                <div class="book-header" role="navigation">

                </div>


                 -->

                <div class="page-wrapper" tabindex="-1" role="main">
                    <div class="page-inner">

                        <div id="book-search-results">
                            <div class="search-noresults">

                                <section class="normal markdown-section">

                                    <h1 id="条件渲染">条件渲染</h1><h3 id="wxif">wx:if</h3><p>在MINA中，我们用<code>wx:if="{{condition}}"</code>来判断是否需要渲染该代码块：</p><pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">view</span> <span class="hljs-attr">wx:if</span>=<span class="hljs-string">"{{condition}}"</span>&gt;</span> True <span class="hljs-tag">&lt;/<span class="hljs-name">view</span>&gt;</span></code></pre>
                                    <p>也可以用<code>wx:elif</code>和<code>wx:else</code>来添加一个else块：</p><pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">view</span> <span class="hljs-attr">wx:if</span>=<span class="hljs-string">"{{length &gt; 5}}"</span>&gt;</span> 1 <span class="hljs-tag">&lt;/<span class="hljs-name">view</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">view</span> <span class="hljs-attr">wx:elif</span>=<span class="hljs-string">"{{length &gt; 2}}"</span>&gt;</span> 2 <span class="hljs-tag">&lt;/<span class="hljs-name">view</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">view</span> <span class="hljs-attr">wx:else</span>&gt;</span> 3 <span class="hljs-tag">&lt;/<span class="hljs-name">view</span>&gt;</span></code></pre>
                                    <h3 id="block-wxif">block wx:if</h3><p>因为<code>wx:if</code> 是一个控制属性，需要将它添加到一个标签上。但是如果我们想一次性判断多个组件标签，我们可以使用一个<code>&lt;block/&gt;</code>标签将多个组件包装起来，并在上边使用<code>wx:if</code>控制属性。</p><pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">block</span> <span class="hljs-attr">wx:if</span>=<span class="hljs-string">"{{true}}"</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">view</span>&gt;</span> view1 <span class="hljs-tag">&lt;/<span class="hljs-name">view</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">view</span>&gt;</span> view2 <span class="hljs-tag">&lt;/<span class="hljs-name">view</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">block</span>&gt;</span></code></pre>
                                    <p><strong>注意：</strong> <code>&lt;block/&gt;</code>并不是一个组件，它仅仅是一个包装元素，不会在页面中做任何渲染，只接受控制属性。</p><h3 id="wxif-vs-hidden"><code>wx:if</code> vs <code>hidden</code></h3><p>因为<code>wx:if</code>之中的模板也可能包含数据绑定，所有当<code>wx:if</code>的条件值切换时，MINA有一个局部渲染的过程，因为它会确保条件块在切换时销毁或重新渲染。</p><p>同时<code>wx:if</code>也是<strong>惰性的</strong>，如果在初始渲染条件为<code>false</code>，MINA什么也不做，在条件第一次变成真的时候才开始局部渲染。</p><p>相比之下，<code>hidden</code>就简单的多，组件始终会被渲染，只是简单的控制显示与隐藏。</p><p>一般来说，<code>wx:if</code>有更高的切换消耗而<code>hidden</code>有更高的初始渲染消耗。因此，如果需要频繁切换的情景下，用<code>hidden</code>更好，如果在运行时条件不大可能改变则<code>wx:if</code>较好。</p>

                                </section>

                            </div>
                            <div class="search-results">
                                <div class="has-results">

                                    <h1 class="search-results-title"><span class="search-results-count"></span> 个结果 "<span class="search-query"></span>"</h1>
                                    <ul class="search-results-list"></ul>

                                </div>
                                <div class="no-results">

                                    <h1 class="search-results-title">没有找到相关内容 "<span class="search-query"></span>"</h1>

                                </div>
                            </div>
                        </div>

                    </div>
                </div>
                <div class="foot" id="footer">
                    <ul class="links ft">
                        <li class="links_item"><a href="">关于腾讯</a></li>
                        <li class="links_item"><a href="">服务协议</a></li>
                        <li class="links_item"><a href="">运营规范</a></li>
                        <li class="links_item"><a href="">辟谣中心</a></li>
                        <li class="links_item"><a href="">客服中心</a></li>
                        <li class="links_item"><a href="">联系邮箱</a></li>
                        <li class="links_item"><a href="">侵权投诉</a></li>
                        <li class="links_item">
                            <p class="copyright">Copyright © 2012-<span id="s_copyright_year"></span> Tencent. All Rights Reserved.</p>
                        </li>
                    </ul>
                </div>

            </div>



            <a href="data.html" class="navigation navigation-prev " aria-label="Previous page: 数据绑定">
                <i class="fa fa-angle-left"></i>
            </a>


            <a href="list.html" class="navigation navigation-next " aria-label="Next page: 列表渲染">
                <i class="fa fa-angle-right"></i>
            </a>



        </div>
    </div>


    <script src="../../../../gitbook/gitbook.js"></script>
    <script src="../../../../gitbook/theme.js"></script>


    <script src="../../../../gitbook/gitbook-plugin-search/search-engine.js"></script>



    <script src="../../../../gitbook/gitbook-plugin-search/search.js"></script>



    <script src="../../../../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>



    <script src="../../../../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>


    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"条件渲染","level":"1.2.4.1.2","depth":4,"next":{"title":"列表渲染","level":"1.2.4.1.3","depth":4,"path":"framework/view/wxml/list.md","ref":"framework/view/wxml/list.md","articles":[]},"previous":{"title":"数据绑定","level":"1.2.4.1.1","depth":4,"path":"framework/view/wxml/data.md","ref":"framework/view/wxml/data.md","articles":[]},"dir":"ltr"},"config":{"gitbook":"*","theme":"default","variables":{},"plugins":[],"pluginsConfig":{"highlight":{},"search":{},"lunr":{"ignoreSpecialCharacters":false,"maxIndexSize":1000000},"theme-default":{"showLevel":false,"styles":{"ebook":"styles/ebook.css","epub":"styles/epub.css","mobi":"styles/mobi.css","pdf":"styles/pdf.css","print":"styles/print.css","website":"styles/website.css"}}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"styles":{"ebook":"styles/ebook.css","epub":"styles/epub.css","mobi":"styles/mobi.css","pdf":"styles/pdf.css","print":"styles/print.css","website":"styles/website.css"}},"file":{"path":"framework/view/wxml/conditional.md","mtime":"2016-09-21T04:20:27.000Z","type":"markdown"},"gitbook":{"version":"3.2.0","time":"2016-09-22T09:40:22.799Z"},"basePath":"../../..","book":{"language":""}});
        });
    </script>




</div></body></html>